<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Products type manager
 *
 * @author	Tran Van Thanh
 * @mail	thanhtran@vietnambiz.com
 * @date	30.08.2011
 */
require_once('admin'.EXT);

class Product_type extends Admin {

	function __construct()
	{
		parent::__construct();
		$this->load->model(array('product_type_model', 'user_model'));
		$this->load->helper('upload');		
	}

	function index()
	{
		$page = (isset($_GET['p']) && is_numeric($_GET['p'])) ? $_GET['p'] : 1;

        if(isset($_GET['status'])) {
            $status = ($_GET['status'] == 'active') ? 'yes' : 'no';
            $this->db->where('status', $status);
        }

		$this->data['product_type'] 	= $this->product_type_model->get_product_type($page != "" ? $page : 1);		
		$this->data['page'] 	= $page;

    	$conf =	array(
    		'base_url'		=> admin_url($this->router->class).'/',
    		'total_rows'	=> $this->product_type_model->count_all(),
    		'per_page'		=> $this->config->item('product_type', 'limit'),
    		'cur_page'		=> $page,
    	);
    	$this->pagination->initialize($conf);
    	$this->data['pagnav'] = $this->pagination->display_query_string();

		if($this->input->is_ajax_request()) {
			$this->load->view('product_type/list', $this->data);
		}
		else {
			$this->data['tpl_file']	= 'product_type/index';
			$this->load->view('layout/default', $this->data);
		}
	}

	function add()
	{		
		if($_SERVER['REQUEST_METHOD'] == 'POST') 
		{
    		$this->form_validation->set_rules('name', 'Tên loại sản phẩm', 'required|trim|xss_clean');    		                    

    		if($this->form_validation->run() == TRUE)
            {            	
            	$name = $this->input->post('name');

            	$data = array(
            		'name'				=> $name,
            		'name_link'			=> ascii_link($name,'-'),            		          		
            		'status'			=> $this->input->post('status')
            	);

            	$this->product_type_model->insert($data);            	
            	die('yes');            	
            }
            else {
            	die(validation_errors());
            }
		}
		
		$this->load->view('product_type/add', $this->data);
	}

	function edit($product_type_id = null)
	{
		$product_type = $this->product_type_model->get($product_type_id);
		if(!isset($product_type->id)) {
			$this->message->add('error', 'Không tìm thấy loại sản phẩm nào phù hợp !');
			admin_redirect($this->router->class);
		}	

		if($_SERVER['REQUEST_METHOD'] == 'POST') 
		{
    		$this->form_validation->set_rules('name', 'Tên loại sản phẩm', 'required|trim|xss_clean');    		                      

    		if($this->form_validation->run() == TRUE)
            {       
            	//Update products type
            	$name = $this->input->post('name');

            	$arr = array(
            		'name'				=> $name,
            		'name_link'			=> ascii_link($name,'-'),            		
            		'status'			=> $this->input->post('status')
            	);            	
            	            	
            	$this->product_type_model->update($product_type_id,$arr);
            	die('yes');            	            	            	
            }
            else {
            	die(validation_errors());
            }
		}

		$this->data['product_type'] 	= $product_type;		
		$this->load->view('product_type/edit', $this->data);
	}

	/** 
	 * Delete product type
	 * @author Tran Van Thanh
	 * @date 30.08.2011
	 */
    function delete($product_type_id = null)
    {    	
        if($_SERVER['REQUEST_METHOD'] == 'POST') 
        {        	
            $this->product_type_model->delete_product_type($product_type_id);         
            die('yes');
        }
    }

	function do_action()
	{
		if($_SERVER['REQUEST_METHOD'] == 'POST') {

			$id_list = $this->input->post('id');
			$action = $this->input->post('action');

			if($action == 'delete') {
				$this->delete($id_list);
			}
			elseif($action == 'pending') {
				$this->product_type_model->update_many($id_list, array('status' => 'no'));
			}
			elseif($action == 'active') {
				$this->product_type_model->update_many($id_list, array('status' => 'yes'));
			}

			die('yes');
		}
	}

	function change_status()
	{
		$product_type_id = $this->input->post('product_type_id');
		$status = $this->input->post('status');

		$this->product_type_model->update($product_type_id, array('status' => $status));
		die('yes');
	}

	function load_row($product_type_id = null)
	{		
		$this->data['product_type'] = $this->product_type_model->get($product_type_id);
		$this->load->view('product_type/row', $this->data);
	}	
}

